/**
 * Copyright (C)  Gadglet .
 *
 * This file is part of Gadglet
 *
 * Gadglet is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gadglet is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Gadglet. If not, see <http://www.gnu.org/licenses/>.
 */
package com.gadglet.core;

import java.util.List;
import java.util.logging.Logger;

import com.gadglet.core.GadgletResponse.ContentRecord;
import com.gadglet.data.DomainUser;
import com.gadglet.data.DomainUserFriendsUtils;
import com.gadglet.data.DomainUserUtils;
import com.gadglet.params.ProfileFields;

/**
 * Basic server side Gadget implementation for Google Apps accounts (Bizlet)
 * The class will be used by every request from a Bizlet.
 * Developer must extends this class to implement Bizlet server side code. 
 *
 */
public abstract class BizletRequestHandler extends BasicRequestHandler {
	Logger log = Logger.getLogger(this.getClass().getName());

	@Override
	public void doConfig(GadgletRequestWrapper request, GadgletResponse res)
			throws RequestException {

		// do nothing at the moment, status OK
	}

	/* Creates the JSON object of the current user profile attributes
	 * 
	 * @param request - request
	 * @param res - response
	 * 
	 * */
	@Override
	public void getProfile(GadgletRequestWrapper request, GadgletResponse res)
			throws Exception {

		try {

			DomainUser currUser = request.getCurrentDomainUser();

			ContentRecord profileRecord = res.newItem();

			profileRecord.addItemField(
					ProfileFields.PROFILE_NICKNAME.getParamName(),
					currUser.getNickName());

			profileRecord.addItemField(
					ProfileFields.PROFILE_THUMBNAILURL.getParamName(),
					currUser.getPhotoUrl());

			profileRecord.addItemField(
					ProfileFields.PROFILE_TITLE.getParamName(),
					currUser.getTitle());

		} catch (Exception e) {

			throw e;
		}
	}

	/* updates the current user profile Object
	 * 
	 * @param request - request
	 * @param res - response
	 * 
	 * */
	@Override
	public void updateProfile(GadgletRequestWrapper request, GadgletResponse res)
			throws Exception {

		try {

			DomainUserUtils.updateMyUserInfo(request);

		} catch (Exception e) {

			throw e;
		}
	}

	/* Creates the JSON objects of the current user frinds list
	 * 
	 * @param request - request
	 * @param res - response
	 * 
	 * */
	public void doGetFriends(GadgletRequestWrapper request, GadgletResponse res)
			throws Exception {

		
		List<DomainUser> myFriendsList = DomainUserFriendsUtils
				.getMyfriends(request);
		
		if (myFriendsList != null) {
		
			for (DomainUser friend : myFriendsList) {
				ContentRecord profileRecord = res.newItem();
				
				profileRecord.addItemField(
						ProfileFields.PROFILE_NICKNAME.getParamName(),
						friend.getNickName());

				profileRecord.addItemField(
						ProfileFields.PROFILE_THUMBNAILURL.getParamName(),
						friend.getPhotoUrl());

				profileRecord.addItemField(
						ProfileFields.PROFILE_TITLE.getParamName(),
						friend.getTitle());
				
				profileRecord.addItemField(
						ProfileFields.PROFILE_FRIEND_ID.getParamName(),
						friend.getUniqueId());

			}

			res.setTotalItemsNum(myFriendsList.size());

		}

	}
	
	/* Creates an new friend for the current user 
	 * 
	 * @param request - request
	 * @param res - response
	 * 
	 * */

	public void doInvite(GadgletRequestWrapper request, GadgletResponse res)
			throws Exception {
		
		DomainUserFriendsUtils.addFreind(request);

	}
	
	/* Creates the JSON objects of the current user Domain users
	 * 
	 * @param request - request
	 * @param res - response
	 * 
	 * */

	public void doGetDomainUsers(GadgletRequestWrapper request,
			GadgletResponse res) throws Exception {

		List<DomainUser> myDomainUsers = DomainUserUtils.getMyDomainUsers(
				request.getCurrentDomainUser(), request.getOrderBy() ,
				request.getStartFrom(), request.getItemNum());

		int count = 0;
		if (myDomainUsers != null) {
			for (DomainUser friend : myDomainUsers) {
				// skip current user from list
				if(friend.getUniqueId().equals(request.getCurrentDomainUserId()))
					continue;
				
				ContentRecord profileRecord = res.newItem();

				count ++;
				profileRecord.addItemField(
						ProfileFields.PROFILE_NICKNAME.getParamName(),
						friend.getNickName());

				profileRecord.addItemField(
						ProfileFields.PROFILE_THUMBNAILURL.getParamName(),
						friend.getPhotoUrl());

				profileRecord.addItemField(
						ProfileFields.PROFILE_TITLE.getParamName(),
						friend.getTitle());
				
				profileRecord.addItemField(
						ProfileFields.PROFILE_FRIEND_ID.getParamName(),
						friend.getUniqueId());

			}

			res.setTotalItemsNum(count);

		}

	}

	/* Remove friend from the current user friends list
	 * 
	 * @param request - request
	 * @param res - response
	 * 
	 * */
	public void doRemoveFriend(GadgletRequestWrapper request,
			GadgletResponse res) throws Exception {
		DomainUserFriendsUtils.removeFriend(request);

	}

}
